package voldTransfo;


import static org.junit.Assert.*;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.uml2.uml.Interface;
import org.eclipse.uml2.uml.Operation;
import org.eclipse.uml2.uml.Class;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import uml2Java.IllegalVisibityException;
import uml2Java.JavaIncompatibleTypeException;
import astjava.AbstractTypeDeclaration;
import astjava.JavaFactory;
import astjava.MethodDeclaration;

public class OldTestTransformMethodAndAccesseur {

	private org.eclipse.uml2.uml.Package _umlPackage;
	
	private astjava.Package _javaPackage;
	
	private OldUML2Java _uml2Java;

	Map<String,String> _correspondanceTypesPrimitifs;
	
	@Before
	public void setUp() throws Exception {
		_uml2Java = new OldUML2Java("modele_de_test/testMethod.uml");
		_umlPackage = _uml2Java.get_UMLpackageRacine();
		_javaPackage = JavaFactory.eINSTANCE.createPackage();
		try {
			_uml2Java.transformTypes(_umlPackage, _javaPackage);
		} catch (JavaIncompatibleTypeException e) {
			e.printStackTrace();
			fail();
		} catch (IllegalVisibityException e) {
			e.printStackTrace();
			fail();
		}
		_correspondanceTypesPrimitifs = new HashMap<String,String>();
		_correspondanceTypesPrimitifs.put("EBoolean", "boolean");
		_correspondanceTypesPrimitifs.put("EByte", "byte");
		_correspondanceTypesPrimitifs.put("EChar", "char");
		_correspondanceTypesPrimitifs.put("EDouble", "double");
		_correspondanceTypesPrimitifs.put("EFloat", "float");
		_correspondanceTypesPrimitifs.put("EInt", "int");
		_correspondanceTypesPrimitifs.put("ELong", "long");
		_correspondanceTypesPrimitifs.put("EShort", "short");
		_correspondanceTypesPrimitifs.put("EString", "String");
	}

	@Test
	public void testTransformMethodInterface() {
		Interface umlInterface = (Interface) _umlPackage.getOwnedType("Interface");
		astjava.InterfaceDeclaration javaInterface = null;
		for (AbstractTypeDeclaration type : _javaPackage.getOwnedElements()) {
			if (type.getName().equals("Interface")) {
				javaInterface = (astjava.InterfaceDeclaration) type;
			}
		}
		assertNotNull(umlInterface);
		assertNotNull(javaInterface);
		assertTrue(umlInterface.getOperations().size() == javaInterface.getBodyDeclarations().size());
		for (Operation umlMethod : umlInterface.getOperations()) {
			boolean trouve = false;
			for (astjava.BodyDeclaration javaMethod : javaInterface.getBodyDeclarations()) {
				if (javaMethod.getName().equals(umlMethod.getName())) {
					trouve = true;
					MethodDeclaration methode = (MethodDeclaration) javaMethod;
					assertEquals(methode.getModifier().getVisibility(),astjava.VisibilityKind.PUBLIC);
					assertEquals(methode.getModifier().getInheritance(), astjava.InheritanceKind.ABSTRACT);
				}	
			}
			assertTrue(trouve);
		}
	}

	@Test
	public void testTransformMethodClass() {
		Class umlClass = (Class) _umlPackage.getOwnedType("ClassConcrete");
		astjava.ClassDeclaration javaClass = null;
		for (AbstractTypeDeclaration type : _javaPackage.getOwnedElements()) {
			if (type.getName().equals("ClassConcrete")) {
				javaClass = (astjava.ClassDeclaration) type;
			}
		}
		assertNotNull(umlClass);
		assertNotNull(javaClass);
		assertTrue(umlClass.getOperations().size() == javaClass.getBodyDeclarations().size());
		for (Operation umlMethod : umlClass.getOperations()) {
			boolean trouve = false;
			for (astjava.BodyDeclaration javaMethod : javaClass.getBodyDeclarations()) {
				if (javaMethod.getName().equals(umlMethod.getName())) {
					trouve = true;
				}
			}
			assertTrue(trouve);
		}
	}
	
//	@Test
//	public void testTransformReturnType() {
//		
//
//		Class umlClass = (Class) _umlPackage.getOwnedType("ClassParametre");
//		astjava.ClassDeclaration javaClass = null;
//		for (AbstractTypeDeclaration type : _javaPackage.getOwnedElements()) {
//			if (type.getName().equals("ClassParametre")) {
//				javaClass = (astjava.ClassDeclaration) type;
//			}
//		}
//		assertNotNull(umlClass);
//		assertNotNull(javaClass);
//		assertTrue(umlClass.getOperations().size() == javaClass.getBodyDeclarations().size());
//		for (Operation umlMethod : umlClass.getOperations()) {
//			boolean trouve = false;
//			boolean typeConcordant = false;
//			for (astjava.BodyDeclaration javaMethod : javaClass.getBodyDeclarations()) {
//				if (javaMethod.getName().equals(umlMethod.getName())) {
//					trouve = true;
//					MethodDeclaration methode = (MethodDeclaration) javaMethod;
//					String javaReturn = methode.getReturnType().getType().getName();
//					String umlReturn = umlMethod.getType().getName();
//					typeConcordant = javaReturn.equals(umlReturn) || _correspondanceTypesPrimitifs.get(umlReturn).equals(javaReturn);
//				}
//			}
//			assertTrue(trouve);
//			assertTrue(typeConcordant);
//		}
//	}
	
	@After
	public void tearDown() throws Exception {
		_uml2Java = null;
		_umlPackage = null;
		_javaPackage = null;

	}

}
